//
//  SLStatusPictureView.swift
//  Sina15
//
//  Created by 盼 on 16/4/14.
//  Copyright © 2016年 pan. All rights reserved.
//

import UIKit

//设置cell的重用ID
private let cellID = "picture"
//设置图片间的间距
private let picMargin: CGFloat = 3
//配图视图的最大宽度
private let maxW = screenWidth - 2 * cellMargin
//设置item的宽度
private let itemW = (maxW - (2 * picMargin)) / 3

class SLStatusPictureView: UICollectionView {
    
    //模型数组
    var pic_URLS: [SLStatusPicture]?{
        didSet {
//            print(pic_URLS?.count)
            
            let pSize = calculatePicSize()
            
            //设置collectionView的尺寸
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(pSize)
            }
            
           textLabel.text = "\(pic_URLS?.count ?? 0)"
            
            //刷新页面数据
            self.reloadData()
        }
    }

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        //流水布局
        let flowLayout = UICollectionViewFlowLayout()
        
        //设置流水布局
//        flowLayout.itemSize = CGSize(width: itemW, height: itemW)
        
        //设置行间距和列间距与图片间间距相等
        flowLayout.minimumInteritemSpacing = picMargin
        flowLayout.minimumLineSpacing = picMargin
        
        super.init(frame: frame, collectionViewLayout: flowLayout)
        
        self.backgroundColor = UIColor.whiteColor()
        
        //注册cell
        self.registerClass(SLStatusPictureCell.self, forCellWithReuseIdentifier: cellID)
        
        //设置collection的数据源
        self.dataSource = self

        //设置UI界面
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func calculatePicSize()->CGSize{
        
        let count = pic_URLS?.count ?? 0

        //没有图片
        if count == 0 {
            
            return CGSizeZero
        }
        
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout

        //1张图片
        if count == 1{
            
            let  imageSize = CGSize(width: 100, height: 150)
            //单张图片的时候把图片大小设置和流水布局的itemSize大小一致
             layout.itemSize = imageSize
             return imageSize
        }
        
        layout.itemSize = CGSize(width: itemW, height: itemW)
        
        //4张图片
        if count == 4 {
            let width = 2 * itemW + picMargin
            let height = width
            
            return CGSize(width: width, height: height)
        }
        
        //其他张图片,程序走到这里，一定是其他的多张图片
        /*
        1，2，3 －－1
        4，5，6 －－2
        7，8，9 －－3
        */
        let row = (count - 1)/3 + 1
        let height = CGFloat(row) * itemW + CGFloat(row - 1) * picMargin

        return CGSize(width: maxW, height: height)

    }
    
    //设置UI界面
    func setupUI(){
        
        addSubview(textLabel)
        
        textLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
    }

    //图标个数标签
    private lazy var textLabel: UILabel = UILabel(labelText: "", color: UIColor.redColor(), fontsize: 16)
}

//MARK: 实现数据源方法
extension SLStatusPictureView: UICollectionViewDataSource {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return self.pic_URLS?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellID, forIndexPath: indexPath) as! SLStatusPictureCell
        
        cell.statusPicture = pic_URLS![indexPath.row]
        
        return cell
    }
}
